package com.haotunet.app.goldtreasure.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.db.DBOpenHelper;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.other.Interface.OnPassBackListener;
import com.haotunet.app.goldtreasure.utils.ActivityUtils;
import com.haotunet.app.goldtreasure.utils.Config;
import com.haotunet.app.goldtreasure.utils.Strs;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.goldtreasure.utils.Utils;
import com.haotunet.app.goldtreasure.utils.XEditUtils;
import com.haotunet.app.goldtreasure.views.TitleView;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.tongdun.android.shell.FMAgent;

public class ForgetTradePwdAct extends BaseActivity {

    @InjectView(R.id.title_view)
    TitleView titleView;
    @InjectView(R.id.phone_txt)
    TextView phoneTxt;
    @InjectView(R.id.et_code)
    EditText etCode;
    @InjectView(R.id.btn_get_vericode)
    TextView btnGetVericode;
    @InjectView(R.id.et_id_card)
    EditText etIdCard;
    private DBOpenHelper helper;
    private int curTime;
    private Timer timer;
    public static ForgetTradePwdAct instance = null;
    private Handler doActionHandler = new Handler() {
        @SuppressWarnings("deprecation")
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    if (curTime == 0) {
                        timer.cancel();
                        btnGetVericode.setText("重新获取");
                        btnGetVericode.setTextColor(ForgetTradePwdAct.this.getResources().getColor(R.color.title_bg));
                        btnGetVericode.setEnabled(true);
                    } else {
                        btnGetVericode.setText(curTime + "s" + getString(R.string.repeat_after_sixty_seconds));
                        btnGetVericode.setTextColor(ForgetTradePwdAct.this.getResources().getColor(R.color.title_bg));
                    }
                    break;

                default:
                    break;
            }
        }

        ;
    };
    private int source;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        instance = this;
        setContentView(R.layout.activity_forget_trade_pwd);
        ButterKnife.inject(this);
        helper = new DBOpenHelper(this);
        initView();
        getChangeTradingPwdCode();
    }

    private void initView() {
        source = getIntent().getIntExtra("source", 0);
        titleView.getTitletextView().setText(getString(R.string.change_trade_pwd));
        titleView.getTitletextView().setVisibility(View.VISIBLE);
        titleView.getBackView().setVisibility(View.VISIBLE);
        titleView.getBackView().setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                moveTaskToBack(true);
                overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
            }
        });
        XEditUtils.set(etIdCard, Strs.LETTER_NUMBER, null);
    }

    private void getChangeTradingPwdCode() {
        String blackBox = FMAgent.onEvent(this);
        String imei = Config.getImei(ForgetTradePwdAct.this);
        ActionUtil actionUtil = new ActionUtil(ForgetTradePwdAct.this);
        actionUtil.getFindPwdCode("", blackBox, imei, 2);
        actionUtil.setOnPassBackListener(new OnPassBackListener() {

            @Override
            public void passBack(BaseModel model) {
                if (isFinishing()) {
                    return;
                }
                if (model == null) {
                    UIHelper.showToast(ForgetTradePwdAct.this, getString(R.string.rank_fail));
                    return;
                }
                if (model.getErrorMsg() != null) {
                    UIHelper.showToast(ForgetTradePwdAct.this, model.getErrorMsg());
                } else {
                    phoneTxt.setText(Utils.hideMidFourPhoneNum(helper.getMobile()));
                    setTimerTask();
                    btnGetVericode.setEnabled(false);
                }
            }
        });
    }

    private void checkChangeTradingPwdInfo(final String code, final String idNum) {
        ActionUtil actionUtil = new ActionUtil(ForgetTradePwdAct.this);
        actionUtil.checkChangeTradingPwdInfo(code, idNum);
        actionUtil.setOnPassBackListener(new OnPassBackListener() {

            @Override
            public void passBack(BaseModel model) {
                if (isFinishing()) {
                    return;
                }
                if (model == null) {
                    UIHelper.showToast(ForgetTradePwdAct.this, getString(R.string.rank_fail));
                    return;
                }
                if (model.getErrorMsg() != null) {
                    if (model.getErrorCode().equals(Strs.EXTRUSION_SIGN_IN)) {
                        ActivityUtils.forceQuit(ForgetTradePwdAct.this, getString(R.string.crowding_in_tips_txt));
                    } else {
                        UIHelper.showToast(ForgetTradePwdAct.this, model.getErrorMsg());
                    }
                } else {
                    Intent intent = new Intent();
                    intent.setClass(ForgetTradePwdAct.this, ResetPwdActivity.class);
                    intent.putExtra("type", source);
                    intent.putExtra("code", code);
                    startActivity(intent);
                    finish();
                }
            }
        });
    }

    private void setTimerTask() {
        curTime = 60;
        timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {

                Message msg = new Message();
                msg.what = 1;
                doActionHandler.sendMessage(msg);
                curTime--;
            }
        }, 0, 1000);
    }

    @OnClick({R.id.vericode_txt, R.id.btn_get_vericode, R.id.btn_next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.vericode_txt:
                break;
            case R.id.btn_get_vericode:
                getChangeTradingPwdCode();
                break;
            case R.id.btn_next:
                String code = etCode.getText().toString().trim();
                String idNum = etIdCard.getText().toString().trim();
                if (TextUtils.isEmpty(code)) {
                    UIHelper.showToast(ForgetTradePwdAct.this, "请输入短信验证码!");
                    return;
                }
                if (TextUtils.isEmpty(idNum)) {
                    UIHelper.showToast(ForgetTradePwdAct.this, "请输入身份证号!");
                    return;
                }
                checkChangeTradingPwdInfo(code, idNum);
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            moveTaskToBack(true);
            overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
